package com.cf.studiomanage.controller;

import com.cf.studiomanage.entity.Activity;
import com.cf.studiomanage.service.ActivityService;
import com.cf.studiomanage.util.R;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/activities")
@Tag(name = "活动模块", description = "社团活动发布、报名、签到接口")
public class ActivityController {

    @Autowired
    private ActivityService service;

    @PostMapping("/create")
    @Operation(summary = "创建活动", description = "管理员创建新活动")
    public R create(@RequestBody Activity activity) {
        return service.create(activity) ? R.ok("创建成功") : R.error("创建失败");
    }

    @PostMapping("/update")
    @Operation(summary = "更新活动", description = "管理员更新活动信息")
    public R update(@RequestBody Activity activity) {
        return service.update(activity) ? R.ok("更新成功") : R.error("更新失败");
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除活动", description = "管理员根据 ID 删除活动")
    public R delete(@PathVariable Long id) {
        return service.delete(id) ? R.ok("删除成功") : R.error("删除失败");
    }

    @GetMapping("/list")
    @Operation(summary = "活动列表", description = "获取全部活动列表")
    public R list() {
        return R.ok().data("list", service.list());
    }

    @GetMapping("/{id}")
    @Operation(summary = "活动详情", description = "根据 ID 获取活动详情")
    public R detail(@PathVariable Long id) {
        return R.ok().data("activity", service.getById(id));
    }

    @PostMapping("/register")
    @Operation(summary = "用户报名活动", description = "用户提交活动报名")
    public R register(@RequestBody Map<String, Long> map) {
        return service.register(map.get("userId"), map.get("activityId")) ? R.ok("报名成功") : R.error("报名失败");
    }

    @PostMapping("/leave")
    @Operation(summary = "用户请假", description = "提交请假原因")
    public R leave(@RequestBody Map<String, Object> map) {
        Long userId = Long.valueOf(map.get("userId").toString());
        Long activityId = Long.valueOf(map.get("activityId").toString());
        String reason = map.get("reason").toString();
        return service.requestLeave(userId, activityId, reason) ? R.ok("请假成功") : R.error("请假失败");
    }

    @PostMapping("/signin")
    @Operation(summary = "用户签到", description = "输入签到码进行签到")
    public R signIn(@RequestBody Map<String, Object> map) {
        Long userId = Long.valueOf(map.get("userId").toString());
        Long activityId = Long.valueOf(map.get("activityId").toString());
        String signCode = map.get("signCode").toString();
        return service.signIn(userId, activityId, signCode) ? R.ok("签到成功") : R.error("签到失败");
    }

    @GetMapping("/registrations/{activityId}")
    @Operation(summary = "查看报名情况", description = "管理员获取某活动报名列表")
    public R registrations(@PathVariable Long activityId) {
        return R.ok().data("list", service.getRegistrations(activityId));
    }
}
